<template>
  <div class="add-role-authority">
      <div class="container">
          <div>
              <div  class="input-cell">
                  <span class="cell-label">角色编号</span>
                  <el-input style="width:215px" v-model="info.user_number" disabled></el-input>
              </div>
              <div class="input-cell">
                  <span class="cell-label">权限启用</span>
                  <el-switch
                    v-model="info.status"
                    active-color="#13ce66"
                    inactive-color="#ff4949">
                   </el-switch>
              </div>
              <div class="input-cell">
                  <span class="cell-label">权限编号</span>
                  <el-select v-model="info.auth_number" placeholder="请选择审核状态">
                        <el-option
                        v-for="item in authList"
                        :key="item.id"
                        :label="item.rolename"
                        :value="item.id">
                        </el-option>
                  </el-select>
              </div>
              
              <div style="text-align:center">
                  <el-button style="width:200px;" type="danger" @click="addRoleAuthority">确认修改</el-button>
              </div>
          </div>
      </div>
  </div>
</template>

<script>
export default {
  name:'add-role-authority',
  props:{
      cell:{
          type:Object,
          default:function(){
              return {}
          }
      }
  },
  data () {
    return {
        authList:[],
        info:{
            status:true,
            oldStatus:null,
            old_roleid:null,
            auth_number:null,
            user_number:null
        }
    };
  },
  components: {},
  created() {
     
  },
  mounted() {
      this.info.user_number = this.cell.userid;
      this.getAuthList();
      this.getUserRole(this.cell.userid);
  },
  methods: {
      getAuthList(){
          let url = 'user/role/find';
          this.$Http.post(url,'').then(res=>{   
              this.authList = res.data.content;
          }).catch(err=>{
              console.log(err);
          })
      },
      getUserRole(userid){
          let url = `user/userrole/find`,
              params = `userId=${userid}`;
         this.$Http.post(url,params).then(res=>{
             if(res.data){
                this.info.old_roleid = this.info.auth_number = res.data.content[0].roleId;  
                this.info.oldStatus = res.data.content[0].status;  
                this.info.status = res.data.content[0].status==='0';
             }
         }).catch(err=>{
            this.$message.error("获取用户权限失败！")
         })
      },
      addRoleAuthority(){
        // 修改用户权限
        //   oldUserId	原始userid
        //   oldRoleId	原始角色id
        //   roleId		将要修改成的角色id
        //   status		状态0启用，1禁用
        // oldStatus

          let token = sessionStorage.getItem(1);
          let url = `user/userrole/newUpdate/${token}`,
              params = `oldUserId=${this.info.user_number}&oldStatus=${this.info.oldStatus}&roleId=${this.info.auth_number}&oldRoleId=${this.info.old_roleid}&status=${this.info.status?'0':'1'}`;
          this.$Http.post(url,params).then(res=>{       
             this.$notify.info({
                    title: '提示',
                    message: res.data.resp_message
             });
             this.getUserRole(this.cell.userid);
            //   if(res.data.resp_code === "000000"){

            //  }
          }).catch(err=>{
              console.log("新增角色权限失败",err);
              this.$notify.error({
                    title: '错误',
                    message: '新增角色权限失败'
                });
          })
      }
  }
}

</script>
<style scoped>
    .add-role-authority{
        min-width: 200px;
        width: 100%;
        height: 250px;
        box-sizing: border-box;
    }
    .container{
        padding: 20px;
    }
    .input-cell{
        display: flex;
        justify-content: flex-start;
        align-items: center;
        padding: 20px 0;
    }
    .cell-label{
        display: inline-block;
        width: 100px;
        text-align: right;
        box-sizing: border-box;
        padding-right: 20px;
    }
</style>